#include <stdio.h>
#include <pthread.h>
#include <unistd.h>

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;

void *sum(void *arg){
    int *sums = (int *)arg;
    pthread_mutex_lock(&mutex);
    *sums = *sums + 1;
    pthread_mutex_unlock(&mutex);
    return NULL;
}
int main(int argc, char const *argv[])
{
    int sums = 0;
    pthread_t thread1[10000];
    for (int i = 0; i < 10000; i++){
        pthread_create(&thread1[i], NULL, sum, &sums);
    }
    //等待所有线程执行完毕
    for (int i = 0; i < 10000; i++){
        pthread_join(thread1[i], NULL);
    }
    printf("sums = %d\n", sums);
    pthread_mutex_destroy(&mutex);
    return 0;
}
